package com.lxf.clouduser.netty;

import com.lxf.clouduser.bean.GroupBean;
import com.lxf.clouduser.bean.UserBean;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author 罗兴发
 * @data 2025/6/13 10:25
 * @description：存储本地数据
 */
@Component
public class LocalData {
    //存储连接的通道 分发消息使用
    public static final List<UserBean> userlist = new ArrayList<>();
    //存储通道和用户id的映射关系 用来获取消息通知的通道
    public static final Map<String, Channel> channelUserUrl = new HashMap<>();
    //在线用户列表
    public static final List<GroupBean> grouplist = new ArrayList<>();
    //群组列表
    public static final ChannelGroup channellist = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    //拿到存储连接通道列表中的通道
    public static List<Channel> getAllChannels() {
        List<Channel> channels = new ArrayList<>();
        Iterator<Channel> iterator = channellist.iterator();
        while (iterator.hasNext()) {
            Channel channel = iterator.next();
            channels.add(channel);
        }
        return channels;
    }

    //通过群组id 取出对应的群组
    public static GroupBean getGroupById(String id) {
        for (GroupBean groupModel : grouplist) {
            if (groupModel.getGroupId().equals(id)) {
                return groupModel;
            }
        }
        return null;
    }
}
